package com.gs.disk.modules.headImgAndWallper.service.mobile;

import cn.hutool.core.util.ObjectUtil;
import com.gs.common.exception.RenException;
import com.gs.common.util.ConvertUtils;
import com.gs.disk.modules.headImgAndWallper.dao.mobile.MobileAPairResourceDao;
import com.gs.disk.modules.headImgAndWallper.dao.mobile.MobileCategoryDao;
import com.gs.disk.modules.headImgAndWallper.dto.APairResourceDto;
import com.gs.disk.modules.headImgAndWallper.dto.ResourceDto;
import com.gs.disk.modules.headImgAndWallper.entity.APairResourceEntity;
import com.gs.disk.modules.headImgAndWallper.entity.CategoryEntity;
import com.gs.disk.service.common.OssService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class MobileAPairResourceService {

    @Resource
    private MobileAPairResourceDao mobileAPairResourceDao;

    @Resource
    private MobileCategoryDao categoryDao;

    @Resource
    private OssService ossService;


    /**
     * 获取分类列表
     * @param categoryName 套图的子分类
     * @return
     */
    public Map<String, List<APairResourceDto>> list(String categoryName) {
        Map<String, List<APairResourceDto>> listMap = mobileAPairResourceDao.aPairResourceListMap(categoryName);

        Set<String> keys = listMap.keySet();


        // 设置文件下载地址
        keys.forEach(key -> {
            List<APairResourceDto> resourceEntities = listMap.get(key);
            resourceEntities.forEach(resourceEntity -> {
                Long headImgId = resourceEntity.getHeadImgId();
                String headImgIdUrl = ossService.getDownloadURL(headImgId);
                resourceEntity.setHeadImgUrl(headImgIdUrl);

                Long backgroundImgId = resourceEntity.getBackgroundImgId();
                String backgroundImgIdURL = ossService.getDownloadURL(backgroundImgId);
                resourceEntity.setBackgroundImgIdUrl(backgroundImgIdURL);
            });
        });

        return listMap;
    }

    /**
     * 上传套图资源
     * @param categoryName
     * @param headImgId
     * @param backgroundImgId
     * @param keyWord
     * @param nickname
     */
    public void uploadAPairResource(String categoryName, Long headImgId, Long backgroundImgId, String keyWord, String nickname) {
        CategoryEntity categoryEntity = categoryDao.getByName(categoryName);
        if(ObjectUtil.isNull(categoryEntity) || ObjectUtil.isNull(categoryEntity.getId())) {
            throw new RenException("当前分类:”" + categoryName + "“不存在");
        }

        // 判断头像图片是否存在
        // 判断背景图片是否存在

        APairResourceEntity entity = new APairResourceEntity();
        entity.setCategoryName(categoryName);
        entity.setCategoryId(categoryEntity.getId());
        entity.setHeadImgId(headImgId);
        entity.setBackgroundImgId(backgroundImgId);
        entity.setKeyword(keyWord);
        entity.setNickname(nickname);

        mobileAPairResourceDao.insert(entity);
    }

    /**
     * 套图资源详情
     * @param id
     * @return
     */
    public APairResourceDto info(Long id) {
        APairResourceEntity entity = mobileAPairResourceDao.selectById(id);
        APairResourceDto dto = ConvertUtils.sourceToTarget(entity, APairResourceDto.class);
        Long backgroundImgId = entity.getBackgroundImgId();
        Long headImgId = entity.getHeadImgId();

        String downloadURL = ossService.getDownloadURL(backgroundImgId);
        String headImgURL = ossService.getDownloadURL(headImgId);
        dto.setBackgroundImgIdUrl(downloadURL);
        dto.setHeadImgUrl(headImgURL);
        return dto;
    }
}
